package com.rulin.asp.business.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.QuestionMyResultRow;
import com.rulin.asp.business.entity.QuestionParam;
import com.rulin.asp.business.log.entity.QuestionLog;
import com.rulin.asp.business.log.service.QuestionLogService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.business.service.QuestionService;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.util.Consists;
import com.rulin.asp.util.Tools;

@Controller
@RequestMapping("/myQuesList")
public class MyQuesListController {

	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private QuestionLogService questionLogService;
	
	@RequestMapping(value = "/quesListPage.html", method = RequestMethod.GET)
	public String quesListPage(Model model){
		setDiv(model);
		return "business/myqueslist";
	}
	
	public void setDiv(Model model){
		model.addAttribute("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addAttribute("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addAttribute("questionSources", dictionaryService.getByModel("question_source"));
		model.addAttribute("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addAttribute("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addAttribute("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addAttribute("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addAttribute("userTypes", dictionaryService.getByModel("user_type"));
		model.addAttribute("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
	}
	
	@RequestMapping(value = "/getList.json", method = RequestMethod.GET)
	@ResponseBody
	public String list(QuestionParam bean,HttpServletRequest request){
		String userAccount = (String) request.getSession().getAttribute("userAccount");
		bean.setUserAccount(userAccount);
		Pair<Integer,List<QuestionMyResultRow>> pair= questionService.getMyQuesListByParam(bean);
		return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
	}
	
	@RequestMapping(value = "/openQuesEditPage.html", method = RequestMethod.GET)
	public String openQuesEditPage(Model model,String quesId){
		setDiv(model);
		Question question = questionService.getQuestion(quesId);
		
		if("2".equals(question.getQuesType())){
			model.addAttribute("quesId", quesId);
			return "redirect:/quesList/openQuesTeacherPage.html";
		}
		if("3".equals(question.getQuesType())){
			if(question.getReplyId() == null || "".equals(question.getReplyId())){
				question.setReplyId(UUID.randomUUID().toString());
			}
			model.addAttribute("form", question);
			if("4".equals(question.getQuestionSource())){//进入图书纠错编辑页面
				model.addAttribute("quesId", quesId);
				return "redirect:/errorQuesList/openMyEditPage.html";
			}
			return "business/myquesedit";
		}else{
			model.addAttribute("form", question);
			return "business/myquesread";
		}
	}
	
	@RequestMapping(value = "/reply.json", method = RequestMethod.POST)
	public void reply(Question question){
		question.setQuesType(Consists.QUES_TYPE_4);
		question.setReplyTime(new Date());
		questionService.saveQuestion(question);
		
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("7");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	/**
	 * 保存回复信息
	 * @param question
	 */
	@RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
	public void saveQuesition(Question question){
		questionService.saveQuestion(question);
		
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("3");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
}
